package com.example.springdataelastci.controller;

import com.example.springdataelastci.config.RabbitmqConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送消息
 *
 * @author heyangyang
 * @since 2023-11-18
 */
@RestController
@Slf4j
public class RabbitMQConsumerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送延迟消息
     *
     * @param message 消息内容
     * @param ttl     延迟的时间 毫秒数
     * @return 是否发送消息成功
     */
    @RequestMapping("/send/delayed/{message}/{ttl}")
    public String sendDelayedMessage(@PathVariable("message") String message,
                                     @PathVariable("ttl") Integer ttl) {
        log.info("开始发送消息了");
        rabbitTemplate.convertAndSend(RabbitmqConfig.DELAYED_EXCHANGE, RabbitmqConfig.DELAYED_ROUTING_KEY,
                "消息来自延迟" + ttl + "ms的消息" + message,
                correlationData -> {
                    correlationData.getMessageProperties().setDelay(ttl);
                    return correlationData;
                });
        return "发送消息成功了";
    }

}
